package com.sicau.smarttraceplatform.utils;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.springframework.validation.BindingResult;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sxt
 * @version : 1.0.0
 */
public class ValidateUtil {


    public static List<MViolationFieldError> parseFieldErrors(BindingResult bindingResult) {
        ArrayList<MViolationFieldError> fieldErrors = new ArrayList<>();
        bindingResult.getFieldErrors().forEach(fieldError ->
                fieldErrors.add(new MViolationFieldError(fieldError.getField(), fieldError.getDefaultMessage())));
        return fieldErrors;
    }


    @Getter
    @Accessors(chain = true)
    @ToString
    @AllArgsConstructor
    @ApiModel("自定义属性校验错误类")
    public static class MViolationFieldError {

        /**
         * 校验发生错误的字段名
         */
        @ApiModelProperty("校验发生错误的字段名")
        private final String field;

        /**
         * 错误信息
         */
        @ApiModelProperty("错误信息")
        private final String errorMessage;
    }
}
